/**
 * 相关工艺单查看的js
 */
import { getQueryString } from 'cupshe-utils'
import { computed, onMounted, reactive } from 'vue'
import { remoteDownloadFile, downloadFile } from 'utils/file.js'
import { message } from 'ant-design-vue'
import {
  cp_card_query_material_post,
  cp_card_query_details_post
} from 'server/modules/4494.js'
import {
  innovate_business_query_material_post,
  innovate_business_query_details_post
} from 'server/modules/3922'
import {
  bulk_sheet_query_material_post,
  bulk_sheet_query_details_post,
  bulk_sheet_export_bulk_post
} from 'server/modules/5019'
import { process_list_file_post_ } from 'server/apis/4599.js'
import {
  process_export_innovate_get,
  process_export_get
} from 'server/modules/4599'

const systemType = getQueryString('systemType')

/**
 * 附件表头
 */
export const attachColumns = [
  {
    title: '文件名称',
    key: 'ossName',
    dataIndex: 'ossName',
    width: '40%'
  },
  {
    title: '上传时间',
    key: 'createTime',
    dataIndex: 'createTime',
    width: '30%'
  },
  {
    title: '操作',
    key: 'opt',
    width: '30%',
    slots: { customRender: 'download' }
  }
]

export const useState = function (attrs) {
  const type = attrs?.type

  const listCpSkcPo = attrs.listCpSkcPo || {}
  const { isSwimSuit, isBulkBind } = useParams(attrs)

  const state = reactive({
    isSwimSuit,
    systemType,
    currentName: '',
    activeKey: 1,
    cp: listCpSkcPo.cp,
    processId: listCpSkcPo.processId,
    type,
    loading: false,
    gongyiDownload: [],
    materialData: {},
    isBulkBind,
    btnLoading: {
      0: false,
      1: false,
      2: false,
      3: false,
      4: false
    }
  })
  return state
}
export const useParams = function (attrs) {
  const type = attrs?.type

  const listCpSkcPo = attrs.listCpSkcPo || {}
  // 说明是大货工艺单
  const isBulkBind = listCpSkcPo?.isBulkBind > -1 || false

  const { processId = '', purchaseTaskId = '', isSwimSuit = 1 } = listCpSkcPo

  return {
    isBulkBind,
    type,
    processId,
    purchaseTaskId,
    isSwimSuit
  }
}

/**
 * 工艺单导出
 * @param {*} state
 * @param {*} attrs
 * @returns
 */
export const useWorkExport = function (
  state,
  attrs,
  config = { immediate: false }
) {
  const { isBulkBind, type, processId, purchaseTaskId, isSwimSuit } =
    useParams(attrs)
  // 工艺单导出参数
  const exportParams = computed(() => {
    if (isBulkBind) {
      return {
        brand: '',
        isSwimSuit, //从列表拿
        queryPo: {
          brand: '',
          bulkRecordId: state.materialData.bulkRecordId,
          purchaseTaskId,
          source: 3
        },
        type: 1,
        supplierId: attrs.supplierId
      }
    }
    return {
      isSwimSuit,
      processId,
      type, // 1 大货  2、报价 3、外发打样
      supplierId: attrs.supplierId
    }
  })
  const exportProcess = () => {
    state.loading = true
    remoteDownloadFile(
      {
        params: exportParams.value,
        url: '/process/export-package'
      },
      () => {
        state.loading = false
      }
    )
  }
  const exportcurrentProcess = async () => {
    state.loading = true
    let result = null
    // 1:供应商门户质检订单导出 2:供应商门户外发打样导出
    if (type === 1) {
      result = await bulk_sheet_export_bulk_post({
        purchaseTaskId,
        bulkRecordId: state.materialData.bulkRecordId,
        source: 2
      })
    } else if (type === 3) {
      if (isSwimSuit === 1) {
        result = await process_export_get({
          processSheetId: processId,
          supplierId: attrs.supplierId
        })
      } else {
        result = await process_export_innovate_get({
          processSheetId: processId,
          supplierId: attrs.supplierId
        })
      }
    }
    state.loading = false
    if (result) {
      downloadFile(result.data)
    } else {
      message.success({
        content: '导出失败！',
        top: '100px',
        maxCount: 1,
        key: 'loading',
        duration: 2
      })
    }
  }
  /**
   * processId : 工艺单id
   * purchaseTaskId： 采购单id
   * source： 1 花稿  2版本图
   * type： 来源 1 订单列表-大货工艺单  2 报价列表-工艺单 3-外发打样-工艺单
   * isSwimSuit： 是否为泳衣 1-泳衣 0-非泳衣
   */
  const exportFile = source => {
    state.btnLoading[source] = true
    remoteDownloadFile(
      {
        url: '/process/export-file',
        params: {
          bulkRecordId: attrs.bulkRecordId,
          purchaseTaskId,
          source,
          processId,
          type,
          isSwimSuit
        }
      },
      () => {
        state.btnLoading[source] = false
      }
    )
  }
  const fetchLoadModal = () => {
    process_list_file_post_(
      {
        bulkRecordId: attrs.bulkRecordId,
        purchaseTaskId,
        processId,
        type,
        isSwimSuit
      },
      state
    )
  }
  onMounted(() => {
    state.gongyiDownload = []
    config.immediate && fetchLoadModal()
  })
  return {
    exportParams,
    fetchLoadModal,
    exportcurrentProcess,
    exportProcess,
    exportFile
  }
}

export const useWorkSheet = function (state, attrs) {
  const { isBulkBind, type, purchaseTaskId, isSwimSuit } = useParams(attrs)

  const materialDetailFunc = computed(() => {
    if (isBulkBind) {
      return bulk_sheet_query_material_post
    } else if (isSwimSuit === 1) {
      return cp_card_query_material_post
    } else if (isSwimSuit === 0) {
      return innovate_business_query_material_post
    }
    return cp_card_query_material_post
  })
  const queryDetailFunc = computed(() => {
    if (isBulkBind) {
      return bulk_sheet_query_details_post
    } else if (isSwimSuit === 1) {
      return cp_card_query_details_post
    } else if (isSwimSuit === 0) {
      return innovate_business_query_details_post
    }
    return cp_card_query_details_post
  })
  // 工艺单参数
  const otherParams = computed(() => {
    if (isBulkBind) {
      return {
        bulkRecordId: attrs.bulkRecordId,
        purchaseTaskId,
        source: 2
      }
    } else if (isSwimSuit === 1) {
      return {}
    } else if (isSwimSuit === 0) {
      return {}
    }
    return attrs.listCpSkcPo
  })
  // 评审意见
  const reviewCommentsType = computed(() => {
    // 打样工艺单
    if (type === 3) {
      return 2
    }
    return 3
  })
  /**
   * 获取material data
   * @param {*} data
   */
  const updateMaterialData = data => {
    if (data) {
      state.materialData = data
    }
  }

  return {
    materialDetailFunc,
    updateMaterialData,
    queryDetailFunc,
    reviewCommentsType,
    otherParams
  }
}
